﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title></title>
    <link href="/Static/demo.css" rel="stylesheet" />
    <script src="/Static/boot.js"></script>
    <link href="/Static/miniui/themes/blue/skin.css" rel="stylesheet" />
    <script src="/Static/js/Guid.js"></script>
    <script src="/Static/js/shareJS.js"></script>
    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
    <body>
        <iframe name="ajaxUpload" style="display: none"></iframe>
        <form id="form1" method="post" action="/AJAX/AjaxService.aspx?method=UploadFile"
              enctype="multipart/form-data" target="ajaxUpload">
            <input class="mini-hidden" name="hfGuid" id="hfGuid" value="" />
            <input class="mini-hidden" name="hfFileName" id="hfFileName" value="" />
            <fieldset style="border: solid 1px #aaa; padding: 3px;">
                <legend>基本信息</legend>
                <div style="padding-left: 11px; padding-bottom: 5px;">
                    <table style=" font-size:12px">
                        <tr>
                            <td>
                                所属分类：
                            </td>
                            <td>
                                <input id="CategoryID" name="CategoryID" class="mini-buttonedit" onbuttonclick="onSelectCategory" allowinput="false"
                                       required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                供应商：
                            </td>
                            <td>
                                <input id="PartsSupplierID" name="PartsSupplierID" class="mini-buttonedit" onbuttonclick="onSelectPartsSupplier" allowinput="false"
                                       required="true" style="width: 200px;" />

                            </td>
                        </tr>
                        <tr>
                            <td>
                                Wisetech 料号：
                            </td>
                            <td>
                                <input id="WisetechPartsNO" name="WisetechPartsNO" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td>
                                供应商料号：
                            </td>
                            <td>
                                <input name="SupplierPartsNO" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                配件名称：
                            </td>
                            <td>
                                <input name="PartsName" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                配件显示名称：
                            </td>
                            <td>
                                <input name="PartsDisplayName" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                材料及规格：
                            </td>
                            <td colspan="3">
                                <input name="MaterialSpecification" class="mini-textarea" style="width: 500px;" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                采购价格：
                            </td>
                            <td>
                                <input name="ProcurementPrice" vtype="float" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                采购货币：
                            </td>
                            <td>
                                <input name="ProcurementCurrency" class="mini-combobox" style="width:200px;" required="true" textfield="text" valuefield="id" emptytext="请选择..."
                                       value="" allowinput="false" shownullitem="true" url="/ConfigData/Currency.txt" nullitemtext="请选择..." />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                销售价格：
                            </td>
                            <td>
                                <input name="MarketPrice" vtype="float" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                销售货币：
                            </td>
                            <td>
                                <input name="MarketCurrency" class="mini-combobox" style="width:200px;" required="true" textfield="text" valuefield="id" emptytext="请选择..."
                                       value="" allowinput="false" shownullitem="true" url="/ConfigData/Currency.txt" nullitemtext="请选择..." />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                交期：
                            </td>
                            <td>
                                <input name="GetGoodsDays" vtype="int" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                帐期：
                            </td>
                            <td>
                                <input name="PayGoodsDays" vtype="int" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                        </tr>
                        <tr>
                            <td align="left">
                                库存：
                            </td>
                            <td>
                                <input name="StockQuantity" vtype="float" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                            <td align="left">
                                最小出售数量：
                            </td>
                            <td>
                                <input name="LeastSellCount" vtype="float" class="mini-textbox" required="true" style="width: 200px;" />
                            </td>
                        </tr>
                        <tr>
                            <td align="left">
                                是否可用：
                            </td>
                            <td>
                                <input name="IsAvailable" class="mini-combobox" style="width:200px;" required="true" textfield="text" valuefield="id" emptytext="请选择..."
                                       value="" allowinput="false" shownullitem="true" url="/ConfigData/IsActive.txt" nullitemtext="请选择..." />
                            </td>

                        </tr>
                        <tr>
                            <td>
                                备注：
                            </td>
                            <td colspan="3">
                                <input name="Remark" class="mini-textarea" style="width: 500px;" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                配件图片：
                            </td>
                            <td colspan="3">
                                <div id="fileGrid" class="mini-datagrid" style="width:750px" idfield="id" allowresize="true"
                                     showpager="false">
                                    <div property="columns">
                                        <div type="indexcolumn">
                                        </div>
                                        <div field="FilePath" visible="false">
                                        </div>
                                        <div field="FileName" name="down" width="200" headeralign="center" align="center">
                                            文件名称
                                        </div>
                                        <div field="FileSize" name="formatSize" width="60" headeralign="center" align="center">
                                            文件大小
                                        </div>
                                        <div field="CreateTime" width="120" headeralign="center" align="center" dateformat="yyyy-MM-dd HH:mm">
                                            上传时间
                                        </div>
                                        <div field="CreateBy" width="80" headeralign="center" align="center">
                                            文件上传人
                                        </div>
                                        <div field="id" name="opt" width="100" headeralign="center" align="center">
                                            文件操作
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td colspan="3">
                                <table style="background-color: #F0F0F0; padding-top: 10px; width: 98%" id="shFile">
                                    <tr>
                                        <td nowrap="nowrap">
                                            <b>上载附件：</b><input type="file" id="fileupload" name="fileupload" style="height: 20px;" /><a class="mini-button" iconcls="icon-upload" plain="true" onclick="upload()">上传</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </div>
            </fieldset>


            <div style="text-align: center; padding: 30px;">
                <a class="mini-button" onclick="onOk" style="width: 60px; margin-right: 20px;">确定</a>
                <a class="mini-button" onclick="onCancel" style="width: 60px;">取消</a>
            </div>
            <input id="hd_action" name="action" class="mini-textbox" style="display:none" />
            <input name="ID" class="mini-hidden" />
            <input id="hidCategortCode" name="hidCategortCode" class="mini-hidden" />
            <input id="hidSupplierCode" name="hidSupplierCode" class="mini-hidden" />
            <input id="hidRandom" name="hidRandom" class="mini-hidden" />
        </form>

        <script type="text/javascript">
            mini.parse();

            var form = new mini.Form("form1");
        
            /**保存数据*********************************************************************/
            function SaveData() {
                var o = form.getData();
                form.validate();
                if (form.isValid() == false) return;

                //upload();

                var json = mini.encode([o]);
                // alert(json);
                var strAction = mini.getbyName("action").getValue();

                $.ajax({
                    url: "/Ajax/AjaxService.aspx?method=EditParts",
                    data: { action: strAction, data: json, ID: mini.getbyName("ID").getValue() },
                    cache: false,
                    success: function (text) {
                        var result = $.parseJSON(text);

                        if (result.res != "操作成功") {
                            mini.alert("操作失败!" + result.error);
                        } else {
                            CloseWindow("save");
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                        CloseWindow();
                    }
                });
            }

            /**加载数据*********************************************************************/
            function SetData(data) {
                if (data.action == "Update") {
                    //跨页面传递的数据对象，克隆后才可以安全使用
                    data = mini.clone(data);

                    mini.getbyName("ID").setValue(data.ID);
                    mini.getbyName("action").setValue(data.action);

                    //查询基本信息
                    $.ajax({
                        url: "/Ajax/AjaxService.aspx?method=GetPartsList&pageIndex=0&pageSize=10",
                        cache: false,
                        data: { id: data.ID },
                        success: function (text) {
                            //将字符串反序列化为JS对象
                            var o = mini.decode(text);
                            //注意此处返回的有data及total两组数据
                            //alert(o.data[0].ID);
                            form.setData(o.data[0]);
                            if (o.data[0].IsAvailable) {
                                mini.getbyName("IsAvailable").setValue("Y");
                            } else {
                                mini.getbyName("IsAvailable").setValue("N");
                            }
                            form.setChanged(false);
                            //alert(o.data[0].CategoryID);

                            mini.get("hfGuid").setValue(o.data[0].Guid);

                            mini.get("CategoryID").setValue(o.data[0].CategoryID);
                            mini.get("CategoryID").setText(o.data[0].CategoryDisplayName);
                            mini.get("hidCategortCode").setValue(o.data[0].CategoryCode);
                        
                            mini.get("PartsSupplierID").setValue(o.data[0].PartsSupplierID);
                            mini.get("PartsSupplierID").setText(o.data[0].SupplierName);
                            mini.get("hidSupplierCode").setValue(o.data[0].SupplierCode);
                            //记录页面模式
                            mini.getbyName("action").setValue(data.action);
                            
                            interval = window.setInterval(function () {
                                loadFile();
                            }, 500);
                        }
                    });
                }
                if (data.action == "Insert") {
                    //跨页面传递的数据对象，克隆后才可以安全使用
                    data = mini.clone(data);
                    mini.getbyName("action").setValue(data.action);
                    mini.getbyName("IsAvailable").setValue("Y");

                    mini.get("hfGuid").setValue(Guid.NewGuid().ToString("N"));

                    InitWisetechNO("", "");

                }
                $.ajax({
                    url: "/Ajax/AjaxService.aspx?method=GetRandomNO",
                    cache: false,
                    success: function (text) {
                        mini.get("hidRandom").setValue(text);
                        if (data.action == "Insert")
                            InitWisetechNO(mini.get("hidCategortCode").getValue(), mini.get("hidSupplierCode").getValue());
                    }
                });

            }
            /**获取表单中相关信息********************************************************************************/
            function GetData() {
                var o = form.getData();
                return o;
            }
            function CloseWindow(action) {
                if (action == "close" && form.isChanged()) {
                    if (confirm("数据被修改了，是否先保存？")) {
                        return false;
                    }
                }
                if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
                else window.close();
            }
            function onOk(e) {
                SaveData();
            }
            function onCancel(e) {
                CloseWindow("cancel");
            }

            function onCloseClick(e) {
                var btnEdit = this;
                btnEdit.setValue("");
                btnEdit.setText("");
            }
            function onSelectCategory(e) {
                var btnEdit = this;
                var parentID = btnEdit.getValue();
                mini.open({
                    url: "/Parts/pub/SelectCategory.html",
                    showMaxButton: false,
                    title: "选择分类",
                    width: 350,
                    height: 450,
                    onload: function () {
                        var iframe = this.getIFrameEl();

                        if (parentID != "") {
                            iframe.contentWindow.SetDefaultSelect({ ParentID: parentID });
                        }
                    },
                    ondestroy: function (action) {
                        if (action == "ok") {
                            var iframe = this.getIFrameEl();
                            var data = iframe.contentWindow.GetData();
                            data = mini.clone(data);
                            if (data) {
                                btnEdit.setValue(data.ID);
                                btnEdit.setText(data.CategoryDisplayName);
                                mini.get("hidCategortCode").setValue(data.CategoryCode);
                            
                                InitWisetechNO(data.CategoryCode, mini.get("hidSupplierCode").getValue());
                            }
                        }
                        if (action == "clear") {
                            btnEdit.setValue("");
                            btnEdit.setText("");
                        }

                    }
                });
            }
            function onSelectPartsSupplier(e) {
                var btnEdit = this;
                mini.open({
                    url: "/Parts/pub/SelectPartsSupplier.html?",
                    showMaxButton: false,
                    title: "选择供应商",
                    width: 800,
                    height: 500,
                    ondestroy: function (action) {
                        if (action == "ok") {
                            var iframe = this.getIFrameEl();
                            var data = iframe.contentWindow.GetData();
                            data = mini.clone(data);
                            if (data) {
                                btnEdit.setValue(data[0].ID);
                                btnEdit.setText(data[0].SupplierName);
                                mini.get("hidSupplierCode").setValue(data[0].SupplierCode);
                                InitWisetechNO(mini.get("hidCategortCode").getValue(), data[0].SupplierCode);
                            }
                        }
                        if (action == "clear") {
                            btnEdit.setValue("");
                            btnEdit.setText("");
                        }

                    }
                });
            }
            function InitWisetechNO(categoryCode, supplierCode) {
                var randomNo = mini.get("hidRandom").getValue();
                var arr = mini.get("WisetechPartsNO").getValue().split('-');
                if (arr.length > 2) {
                    if (arr[0] == '' && arr[2] == '') {
                        mini.get("WisetechPartsNO").setValue(categoryCode + "-" + randomNo + "-" + supplierCode);
                    } else {
                        mini.get("WisetechPartsNO").setValue(categoryCode + "-" + arr[1] + "-" + supplierCode);
                    }
                } else {
                    mini.get("WisetechPartsNO").setValue(categoryCode + "-" + randomNo + "-" + supplierCode);
                }
            }
        
            //上传附件
            function upload() {
                var curFilUpload = "fileupload";

                var fileVal = document.getElementById(curFilUpload).value;
                if (fileVal == "") {
                    alert("请选择文件！");
                    return false;
                }
                mini.get("hfFileName").setValue(curFilUpload);
                if ($.browser.msie) {
                    window.form1.submit();
                } else {
                    $("#form1").submit();
                }

                interval = window.setInterval(function () {
                    loadFile();
                }, 500);

                //上传成功后清空当前路径
                document.getElementById(curFilUpload).outerHTML += '';
            }

            function loadFile() {
                var fileGrid = mini.get("fileGrid");
                var len = fileGrid.getData().length;
                $.ajax({
                    url: "/AJAX/AjaxService.aspx?method=GetFileList",
                    type: "post",
                    data: {
                        guid: mini.get("hfGuid").getValue()
                    },
                    cache: false,
                    success: function (data) {
                        var o = mini.decode(data);
                        if (o.length > len) {
                            window.clearInterval(interval);
                            fileGrid.loadData(o);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }

            function onDelFile(id) {
                if (confirm("您确认要删除该文件吗？")) {
                    $.ajax({
                        url: "/AJAX/AjaxService.aspx?method=DeleteFile",
                        type: "post",
                        cache: false,
                        data: { fileId: id },
                        success: function(text) {
                            var fileGrid = mini.get("fileGrid");
                            var rows = fileGrid.getSelecteds();
                            if (rows.length > 0) {
                                fileGrid.removeRows(rows, true);
                            }
                        }
                    });
                }
            }
            
            function onDownLoad() {
                var fileGrid = mini.get("fileGrid");
                var row = fileGrid.getSelected();
                var filePath = escape(row.FilePath);
                window.open("/Pub/DownLoad.aspx?path=" + filePath);
            }
            
            $(function () {
                mini.parse();

                var fileGrid = mini.get("fileGrid");
                fileGrid.on("drawcell", function (e) {
                    if (e.column.name == "opt") {
                        alert(e.id);
                        e.cellHtml = '<a  href="javascript:void(0)" onclick=onDownLoad()>下载</a>&nbsp;' +
                            '<a  href="javascript:void(0)" onclick=onDelFile(' + e.record.ID + ')>删除</a>';
                    }
                    else if (e.column.name == "down") {
                        e.cellHtml = '<img src="' + e.record.FilePath + '" style="width:40px;height:40px"/>';
                        //'<a  href="javascript:void(0)" onclick=onDownLoad()>' + getFileIcon(e.value) + e.value + '</a>';
                    }
                    else if (e.column.name == "formatSize") {
                        e.cellHtml = FormatFileSize(e.value);
                    }
                });
            });
        </script>
    </body>
</html>
